/**
 * 分类接口
 *
 * @project sky-take-out_1
 * @author MECHREVO
 * @date 2023/12/3 17:20:00
 * @version 1.0
 */
package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.ClassificationFormatException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> list=categoryMapper.page(categoryPageQueryDTO);
        Page<Category> p=(Page<Category>) list;
        PageResult pageResult=new PageResult(p.getTotal(),p.getResult());
        return pageResult;
    }

    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setCreateTime(LocalDateTime.now());
        category.setStatus(StatusConstant.DISABLE);
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        if(Objects.isNull(categoryDTO.getName()) ||
                categoryDTO.getName().length()<2||
                categoryDTO.getName().length()>10){
            throw new ClassificationFormatException("分类名称输入不符，请输入2-20个字符");
        }
    if(Objects.isNull(categoryDTO.getSort())||categoryDTO.getSort()<0||categoryDTO.getSort()>100){
        throw new ClassificationFormatException("分类排序输入不符，请输入0-100的整数");
    }
            categoryMapper.insert(category);
    }

    @Override
    public void deleteById(Long id) {
     Integer count=dishMapper.countByCategoryId(id);
        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
            count=setmealMapper.countByCategoryId(id);

        if(count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类数据
        categoryMapper.deleteById(id);
    }
        /* todo 如果菜品都是起售的状态，套餐不能下架
         */
    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        if(Objects.isNull(categoryDTO.getName()) ||
                categoryDTO.getName().length()<2||
                categoryDTO.getName().length()>10){
            throw new ClassificationFormatException("分类名称输入不符，请输入2-20个字符");
        }

        if(Objects.isNull(categoryDTO.getSort())||!categoryDTO.getSort().toString().matches("[0-9]")||categoryDTO.getSort()<0||categoryDTO.getSort()>100){
            throw new ClassificationFormatException("分类排序输入不符，请输入0-100的整数");
        }
        categoryMapper.update(category);
    }

    @Override
    public void updateById(Long id, Integer status) {
        Category category=new Category();
        category.setId(id);
        category.setStatus(status);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.update(category);
    }

    @Override
    public List<Category> list(Integer type) {
        return  categoryMapper.list(type);
    }
}
